/*
 * Hibiscus is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * Hibiscus is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Hibiscus; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 * Copyright (C) 2003 Tetete - http://tetete.hu.
 */
package hu.tetete.hibiscus.model;

import java.io.Serializable;
import java.util.Map;

import hu.tetete.hibiscus.expression.Expression;
import hu.tetete.hibiscus.propertyvalue.PropertyValue;

/**
 * TODO javadoc.
 * @param <V> The type of the property's value.
 */
public abstract class Property<V extends PropertyValue> implements Serializable {

    /** @see Serializable */
    private static final long serialVersionUID = 1L;

    /** The identifier. */
    protected final Long   id;

    /** The name. */
    protected final String name;

    /** The expression - controls the visibility. */
    protected final Expression expression;

    /**
     * @param id The identifier of the property
     * @param name The name of the property
     * @param expression The expression which controls the visibility.
     */
    protected Property(final Long id, final String name, final Expression expression) {
        this.id         = id;
        this.name       = name;
        this.expression = expression;
    }

    /**
     * @param values The selected values (NonNull)
     * @return <tt>true</tt> if the property is visible.
     */
    public boolean isVisible(final Map<Long, PropertyValue> values) {
        return this.expression.isVisible(values);
    }

    /**
     * @return the id
     */
    public Long getId() {
        return this.id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return this.name;
    }

    /**
     * Restores a property value from the db value.
     * @param propertyValueFromDb The db value
     * @return The property value object
     */
    public abstract V fromDb(final String propertyValueFromDb);

    /**
     * Creates a String what can be stored in database,
     * and from which the {@link #fromDb(String)} can restore the object.
     * @param propertyValue The property value
     * @return The db reprezentation
     */
    public abstract String toDb(final V propertyValue);
}
